package ae.pcfc.cldos.online.web.controller.client.company.nocandletters;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import ae.pcfc.cldos.online.common.ConstantsCommon;
import ae.pcfc.cldos.online.common.UltimusConstants.AdminDecision;
import ae.pcfc.cldos.online.common.delegate.GeneralAdminDelegate;
import ae.pcfc.cldos.online.common.exception.BadAppException;
import ae.pcfc.cldos.online.common.vo.BackOfficeAdminVO;
import ae.pcfc.cldos.online.common.vo.UserVO;
import ae.pcfc.cldos.online.common.vo.nandl.NocLetterVO;
import ae.pcfc.cldos.online.web.Constants;
import ae.pcfc.cldos.online.web.PageType;
import ae.pcfc.cldos.online.web.ViewPath;
import ae.pcfc.cldos.online.web.ViewUtils;
import ae.pcfc.cldos.online.web.model.BackOfficeAdminReview;
import ae.pcfc.cldos.online.web.model.NocAndLettersModel;


/**
 * 
 * @author Sergey Manoylo
 *
 */
@Controller
@RequestMapping(ViewPath.__NOC_LETTERS_BACKOFFICE)
public class NocAndLettersBackofficeContoller extends NocAndLettersCommonController {
	
	private static final String NOC_AND_LETTERS_PATH = "client.company.nocandletters.backoffice.form";
	
	@Autowired
	@Qualifier("generalAdminDelegate")
	private GeneralAdminDelegate generalAdminDelegate;	
	
	
	@RequestMapping(method=RequestMethod.GET, value="/form.htm")
	public ModelAndView form(
			@RequestParam(value = "appId", required = false) String appId,
			@RequestParam(value = "UserName", required = false) String userName,
			@RequestParam(value = "ProcessName", required = false) String processName,
			@RequestParam(value = "TaskID", required = false) String taskID,
			@RequestParam(value = "Incident", required = false) String incident,
			@RequestParam(value = "StepName", required = false) String stepName,
			@ModelAttribute(value = "backOfficeAdminReview") BackOfficeAdminReview adminReview,			
			@ModelAttribute(value="nocAndLettersModel") NocAndLettersModel nocAndLettersModel, 
			ModelMap  model) throws BadAppException{
		
		adminReview.setStrApplicationId(appId);
		adminReview.setStrUserName(userName);
		adminReview.setStrTaskId(taskID);
		adminReview.setStrIncidentNo(incident);
		adminReview.setStrStepName(stepName);
		adminReview.setProcessName(processName);
		UserVO userVO =  getUserVO();
		userVO.setGeneralAdmin(true);
		NocLetterVO nocLetterVO = getNocLetterDelegate().getNocLetterAppInfo(appId, userVO, ConstantsCommon.Steps.DOCUMENT_AND_DATA_VALIDATION);
		getMapper().map(nocLetterVO.getNocLetterDetailsVO(),nocAndLettersModel);
		model.put("comments", nocLetterVO.getApplicationCommentVOs());		
		adminReview.setClientReferenceNo(nocLetterVO.getClientReferenceNo());
		adminReview.setInitiatedBy(nocLetterVO.getInitiatedBy());
		return new ModelAndView(NOC_AND_LETTERS_PATH,model);
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/form.htm")
	public ModelAndView onSubmit(
			HttpServletRequest request,
			ModelMap model,
			@RequestParam(value = "adminComments", required=false) 	String adminComments,			
			@ModelAttribute("nocAndLettersModel") NocAndLettersModel nocAndLettersModel,
			BindingResult errors,
			@ModelAttribute(value = "backOfficeAdminReview") BackOfficeAdminReview adminReview
			) throws Exception {
		
		boolean isSubmitApproval = WebUtils.hasSubmitParameter(request, "MainUserApprove");
		boolean isSubmitReject = WebUtils.hasSubmitParameter(request, "MainUserReject");
		boolean isSubmitCancel = WebUtils.hasSubmitParameter(request, "MainUserCancel");
		
		BackOfficeAdminVO backOfficeAdminVO = new BackOfficeAdminVO();
		mapper.map(adminReview, backOfficeAdminVO);
		
		if(isSubmitApproval){
			backOfficeAdminVO.setStrActionType(AdminDecision.APPROVED.getAdminDecision());
		}else if(isSubmitReject){
			if(adminReview.getAdminComments()==null || adminReview.getAdminComments().isEmpty()){
				errors.reject("admin.comment.required");
			}
			backOfficeAdminVO.setStrActionType(AdminDecision.REJECTED.getAdminDecision());
		} else if(isSubmitCancel) {
			backOfficeAdminVO.setStrActionType(AdminDecision.CANCELLED.getAdminDecision());
		}
		
		backOfficeAdminVO.setStrComments(adminComments);
		ModelAndView mav = null;
		
		if(!errors.hasErrors()){
			String clientRefNo = generalAdminDelegate.reviewByBackOffice(backOfficeAdminVO);
			
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put(Constants.__REFRENCE_NO, clientRefNo); 
			paramMap.put("InitiatedBy", adminReview.getInitiatedBy());
			paramMap.put("Incident", adminReview.getStrIncidentNo());
			paramMap.put("StepName", adminReview.getStrStepName());
			mav =new ModelAndView(ViewUtils.getRedirect(ViewPath.__BACKOFFICE_CONFIRMATION, PageType.SHOW, paramMap));
		} else {
			mav = form(adminReview.getStrApplicationId(), 
					adminReview.getStrUserName(), 
					adminReview.getProcessName(), 
					adminReview.getStrTaskId(), 
					adminReview.getStrIncidentNo(), 
					adminReview.getStrStepName(), adminReview, nocAndLettersModel, model);
		}
		
		return mav;
	}
	
}
